Skip to contents

Mirrors the * operator in R formulas: adds all supplied variables as main effects (if not already present) and all k-way interaction terms for k from 2 up to the number of variables supplied. For example, add_interaction(m, a, b, c) ensures "a", "b", "c", "a:b", "a:c", "b:c", and "a:b:c" are all in the spec; variables already present are not duplicated. To add only the single colon-joined term without main effects or lower-order terms, use add_terms(spec, x1:x2).

Usage

add_interaction(spec, ...)

Arguments

spec

A model_spec object.

...

Variables forming the interaction. Accepts unquoted names, strings, or !!var to inject a variable's value programmatically.

Value

A new model_spec with main effects and all interaction terms added.

Examples

# Variables absent from the spec are added as main effects automatically
model_spec() |> add_interaction(a, b, c)
#> model_spec: ~ a + b + c + a:b + a:c + b:c + a:b:c 

# Variables already in the spec are not duplicated
model_spec(a, b, c) |> add_interaction(a, b, c)
#> model_spec: ~ a + b + c + a:b + a:c + b:c + a:b:c 

# Programmatic use
v <- "c"
model_spec() |> add_interaction(a, b, !!v)
#> model_spec: ~ a + b + c + a:b + a:c + b:c + a:b:c